package com.info.zhiduoduo.weixin.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.constants.system.UserType;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.core.service.common.BaseService;
import com.info.zhiduoduo.repository.dto.notice.NoticeDto;
import com.info.zhiduoduo.repository.entity.company.Company;
import com.info.zhiduoduo.repository.entity.maker.Maker;
import com.info.zhiduoduo.repository.entity.partner.Partner;
import com.info.zhiduoduo.repository.mapper.notice.NoticeMapper;
import com.info.zhiduoduo.weixin.formbean.notice.NoticeListFormBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class NoticeService extends BaseService {

	@Autowired
	NoticeMapper noticeMapper;

	public SmartResult getNoticeList(BaseSimpleFormBean<NoticeListFormBean> formBean) {
		NoticeListFormBean reqdata = formBean.getReqdata();
		if (reqdata == null) return errorParamSmartResult();

		Date startDate = reqdata.getStartDate();
		Date endDate = reqdata.getEndDate();
		String title = reqdata.getTitle();
		int idtype = formBean.getIdtype();

		if(startDate == null) startDate = new Date(0);
		if(endDate == null) endDate = new Date();

		//类型： 1：发给全部 2：发给 创客 3：发给全部企业 4：发给合伙人 5：发给智杰祱创客 " +
		//	"6：发给众包企业 7：发给普通企业 8:众包创客 9:自然人 10:指定创客
		//	11:指定企业 12:指定合伙人 13:指定自然人 -1:测试")
		String type = "0";
		if (idtype == UserType.MAKER) type = "1,2,8,10";
		else if (idtype == UserType.COMPANY) type = "1,3,6,7,11";
		else if (idtype == UserType.PARTNER) type = "1,4,12";
		else if (idtype == UserType.CROWDSOURCE) type = "1,9,13";
		else if (idtype == UserType.MAKER_CROWDSOURCE) type = "1,8,10";

		Page<Object> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
		noticeMapper.getListByTypes(title, type, startDate, endDate, formBean.getUserid());
		return smartResultPage(page);
	}

	public SmartResult getDetail(BaseSimpleFormBean<Integer> formBean) {
		int idtype = formBean.getIdtype();
		int userid = formBean.getUserid();
		Integer reqdata = formBean.getReqdata();
		if (idtype == UserType.MAKER) {
			Maker maker = new Maker().selectById(userid);
			if (maker.getNoticeFlag() == 1) {
				return smartResult(StatusMsg.StatusMsg_127);
			}
		} else if (idtype == UserType.COMPANY) {
			Company company = new Company().selectById(userid);
			if (company.getNoticeFlag() == 1) {
				return smartResult(StatusMsg.StatusMsg_127);
			}
		} else if (idtype == UserType.PARTNER) {
			Partner partner = new Partner().selectById(userid);
			if (partner.getNoticeFlag() == 1) {
				return smartResult(StatusMsg.StatusMsg_127);
			}
		}

		NoticeDto detail = noticeMapper.getDetail(reqdata);
		return smartResult(detail);
	}
}
